package booksys.defer;

import booksys.common.CurrentUser;
import booksys.common.Result;
import booksys.common.Utils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//开发一个Servlet(服务端小程序)类
@WebServlet({
        "/defer/deferList",
        "/defer/deferPermit",
        "/defer/deferBan",
        "/defer/deferHistory"
})//注解配置访问地址 / 发送地址
public class DeferAPI extends HttpServlet {//控制器Servlet交互接口

    private static final Logger logger = LogManager.getLogger(DeferAPI.class);//日志输出

    // 客户端（浏览器）发送数据的方式有多种: get, post, delete, put ...

    /**
     * 负责接收客户端发来的post请求
     *
     * @param request  客户端发来的请求
     * @param response 负责向客户端响应
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        DeferService deferService = new DeferService();
        String path = request.getServletPath();

        if ("/defer/deferPermit".equals(path)) {
            try {
                DeferDto dto = Utils.getBeanFromRequest(DeferDto.class, request);
                deferService.permitBookdefer(dto);

                Utils.outJson(response, Result.success());
            } catch (Exception e) {
                e.printStackTrace();
                Utils.outJson(response, Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage()));
            }
        } else if ("/defer/deferBan".equals(path)) {
            try {
                DeferDto dto = Utils.getBeanFromRequest(DeferDto.class, request);
                deferService.banBookdefer(dto);

                Utils.outJson(response, Result.success());
            } catch (IOException e) {
                e.printStackTrace();
                Utils.outJson(response, Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage()));
            }
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        //创建业务模型对象（服务对象/Service对象），通过业务对象执行业务方法
        DeferService deferService = new DeferService();
        CurrentUser currentUser = (CurrentUser) request.getSession().getAttribute(CurrentUser.SESSION_ATTR_NAME);

        String path = request.getServletPath();
        if ("/defer/deferList".equals(path)) {
            try {
                List<Bookdefer> deferList = deferService.getNewBookdeferList(currentUser);

                Utils.outJson(response, Result.success(deferList));

                Utils.outResult(response, Result.success(deferList));
            } catch (IOException e) {
                e.printStackTrace();
                Utils.outJson(response, Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage()));
            }
        } else if ("/defer/deferHistory".equals(path)) {
            try {
                List<Bookdefer> deferList = deferService.getHistBookdeferList(currentUser);

                Utils.outJson(response, Result.success(deferList));

                Utils.outResult(response, Result.success(deferList));
            } catch (Exception e) {
                e.printStackTrace();
                Utils.outJson(response, Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage()));
            }
        }

    }
}
